<?php

/*
 * Класс для работы с сохранёной версией
 */

class Note_modul  extends CI_Model {

    # id телефона
    public $id_phone;

    public function __construct() {
        // Call the Model constructor
        parent::__construct();
        $this -> load -> library('session');
        $this -> id_phone = $this -> session -> userdata('id_phone');
    }

    /**
     * Добавление нового объявления в базу
     *
     * @param array $data массим с данными для добавление в базу
     * @return int $id уникальный идентификатор объявления
     * @author
     */
    public function save($id = 1, $adsent = 'sflat') {
        $DB2 = $this -> load -> database('adsent', TRUE, TRUE);
        $data = array('user' => $this -> id_phone, 'is_adsent' => $id, 'adsent' => $adsent, 'times' => time(), 'activ' => 1, );
        $DB2 -> insert('notepad', $data);
        return $DB2 -> insert_id();
    }

    /**
     * Загружаем все объявления на которых пользователь поставил метку блокнота
     *
     * @param int пользователь
     * @data array 6 массивов с различными объявлениями
     */
    public function load() {
        $data = array();

        $DB2 = $this -> load -> database('adsent', TRUE, TRUE);

        $this -> load -> model('search/rent/Flat_modul', 'rflat');
        $this -> load -> model('search/rent/Office_modul', 'roffice');
        $this -> load -> model('search/rent/Suburbian_modul', 'rsuburbian');
        $this -> load -> model('search/sale/Flats_modul', 'sflat');
        $this -> load -> model('search/sale/Offices_modul', 'soffice');
        $this -> load -> model('search/sale/Suburbians_modul', 'ssuburbian');

        $select_note = "SELECT `adsent`, count(`id`) FROM `notepad` where `user`= '" . $this -> id_phone . "' and `activ`=1 group by `adsent`  LIMIT 0 , 30";
        $note_data = $DB2 -> query($select_note);

        foreach ($note_data -> result() as $row) {

             $select = "SELECT r.* FROM `" . $row -> adsent . "` r join `notepad` n ON n.is_adsent = r.id   and n.adsent = '" . $row -> adsent . "'    where n.user= '" . $this -> id_phone . "' and n.activ=1  group by r.id  order by r.id LIMIT 0 , 30;";

            $rows[$row -> adsent] = $DB2 -> query($select);

        }

        foreach ($rows as $keys => $row_select) {

            switch ($keys) {
                # Аренда
                case 'rflat' :
                    $ara['rflat'] = $this -> rflat -> req($rows[$keys]);
                    break;
                case 'roffice' :
                    $ara['roffice'] = $this -> roffice -> req($rows[$keys]);
                    break; 
                case 'rsuburbian' : 
                    $ara['rsuburbian'] = $this -> rsuburbian -> req($rows[$keys]);
                   # print_r($ara['rsuburbian']);
                    break;

                # Продажа
                case 'sflat' :
                    $ara['sflat'] = $this -> sflat -> req($rows[$keys]);

                    break;
                case 'soffice' :
                    $ara['soffice'] = $this -> soffice -> req($rows[$keys]);

                    break;
                case 'ssuburbian' :
                    $ara['ssuburbian'] = $this -> ssuburbian -> req($rows[$keys]);

                    break;
                default :
                    break;
            }

        } 


        return $ara;

    }

}
